﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Common.Constants;
using Common.Helper;
using Common.Interface;
using EntityFrameworkExtension;
using EntityFrameworkExtension.Interface;
using MainProcessor.Interface;

namespace MainProcessor.ContextProcessor
{
    public class ReplacedInteface1Processor : BaseContextProcessor<ReplacedInteface1>, IContextProcessor
    {
        private readonly IDataCache dataCache;
        private readonly ILatestParentMeasures latestParentMeasures;
       
        public ReplacedInteface1Processor(IDataController dataController, IDataCache dataCache, ILatestParentMeasures latestParentMeasures)
            : base(dataController)
        {
            this.dataCache = dataCache;
            this.latestParentMeasures = latestParentMeasures;
        }

        public void ExtractXml(XElement xElement)
        {
            List = (from topLevel in xElement.Descendants(XmlConstants.TopLevel)
                        let generationName = topLevel.Element(XmlConstants.InvestibleGeneration)
                        let assetClass = topLevel.Element(XmlConstants.InvestibleAssetClass)
                    from parentContainer in topLevel.Descendants(XmlConstants.ParentContainer)
                        let mappingId = parentContainer.Element(XmlConstants.MappingId)
                        
                        let capacity = latestParentMeasures.Initialise(parentContainer, XmlConstants.ParentMeasure, XmlConstants.LegacyType, ParentMeasureType.AuM.ToDescription())
                        where !mappingId.IsNullOrEmpty()
                    select
                        new ReplacedInteface1
                        {
                            MappingId = NullableValue<int>(mappingId),
                            GenerationName = generationName.IsNullOrEmpty() ? default(String) : generationName.Value,
                            AssetClassName = assetClass.IsNullOrEmpty() ? default(String) : assetClass.Value,
                            CapacityReserved = latestParentMeasures.CapacityReserved,
                            CapacityTotal = latestParentMeasures.TotalCapacity,
                            NoOfEmployees = latestParentMeasures.TotalEmployees
                            
                        }).ToList();
        }

        public void PopulateEf(IEfContext efContext)
        {
            foreach (var fund in List)
            {
                fund.GenerationID = (short)dataCache.GetId<ReferenceTypeTable1, int>(fund.GenerationName, efContext);
                fund.AssetClassID = (short)dataCache.GetId<ReferenceTypeTable2, int>(fund.AssetClassName, efContext);
                if (fund.MappingId != null) fund.Guid = GetGuid(fund.MappingId.Value);
                Populate(fund, efContext);
            }
        }



        protected override ReplacedInteface1 SelectCurrent(ReplacedInteface1 inputFund, IEfContext efContext)
        {
            return efContext.SelectCurrent<ReplacedInteface1>(x => x.Guid == inputFund.Guid);
        }
    }
}
